﻿using BusinessLogicLayer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PresentationLayer
{
    public partial class frmKyHD : Form
    {
        public NhanVien nhanVien;
        public CapNhatDSNV capNhatDS;

        public frmKyHD()
        {
            InitializeComponent();
        }

        private void frmKyHD_Load(object sender, EventArgs e)
        {
            dtpNgayKy.Value = DateTime.Today;
            dtpNgayKy.MinDate = DateTime.Today;
            if (nhanVien != null)
                mtxtChuKyHD.Text = nhanVien.ChuKyHopDong.ToString();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private bool KTraThongTin()
        {
            if (dtpNgayKy.Value == DateTime.MinValue)
            {
                MessageBox.Show("Thông tin ngày ký chưa có!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dtpNgayKy.Focus();
                return false;
            }
            if (mtxtChuKyHD.Text.Trim() == "" || mtxtChuKyHD.Text == "0" || mtxtChuKyHD.Text == "00" || mtxtChuKyHD.Text == " 0")
            {
                MessageBox.Show("Chu kỳ hợp đồng phải lớn hơn 0!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                mtxtChuKyHD.Focus();
                return false;
            }
            return true;
        }

        private void btnKyHD_Click(object sender, EventArgs e)
        {
            if (KTraThongTin())
            {
                nhanVien.NgayKyLaiHD = dtpNgayKy.Value.AddMonths(Convert.ToInt32(mtxtChuKyHD.Text.Trim()));
                nhanVien.ChuKyHopDong = Convert.ToInt32(mtxtChuKyHD.Text.Trim());
                ActionResult ac = new NhanVienManager().CapNhat(ref nhanVien);
                if (ac.Success)
                {
                    MessageBox.Show("Ký hợp đồng thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    capNhatDS(LoaiHanhDong.CapNhat, nhanVien);
                }
                else
                    MessageBox.Show(ac.Exceptions[ac.Exceptions.Count - 1].Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
